<?php
class Image extends AppModel {
	var $name = 'Image';
	var $actsAs = array('Image');
	var $validate = array(
		'name' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'clean_name' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'type' => array(
			'validateTypeImage' => array(
				'rule' => 'validateTypeImage',
				'message' => 'Tipo de imagem inválida.',
				'allowEmpty' => true,
				'required' => false
			),
		),
		'size' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'filehash' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'ext' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'File' => array(
			'isUploadedFile' => array(
				'rule' => array('isUploadedFile'),
				'message' => 'Arquivo Inválido',
				'allowEmpty' => true,
				'required' => false,
			),
			'file_type' => array(
				'rule' => array('check_extension', array(
					'jpg', 'jpeg', 'gif', 'png', 'bmp', 'tiff', //Imagem
					)),
				'message' => 'Tipo de Arquivo Inválido. Arquivo deve ser Imagem.'
			)
		)		
	);
	//The Associations below have been created with all possible keys, those that are not needed can be removed
	var $belongsTo = array(
			'Equipamento' => array(
				'className' => 'Equipamento',
				'foreignKey' => 'equipamento_id',
				'conditions' => '',
				'fields' => '',
				'order' => ''
			)
	);

	public function validateTypeImage($image) {
		
		switch ($image['type']) {
			case 'image/jpeg':
			case 'image/png':
			case 'image/gif':
				return true;
				break;
			default:
				return false;
				break;
		}
		
	}
	
	function isUploadedFile($params)
	{
		$val = array_shift($params);
	
		if((isset($val['error']) && $val['error'] == UPLOAD_ERR_NO_FILE)) //Nenhum arquivo anexado
			return true;
	
		if (
				(isset($val['error']) && $val['error'] == 0)
				||
				(!empty( $val['tmp_name']) && $val['tmp_name'] != 'none')
		)
		{
			return isset($val['conteudo']) || is_uploaded_file($val['tmp_name']);
		}
	
		return false;
	}	
}
